package com.homework.module6part1.service.impl;

import com.homework.dao.domain.Order;
import com.homework.dao.mapper.OrderMapper;
import com.homework.module6part1.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    public Order addOrder() {
        Order order = new Order();
        order.setUser("john");
        Random random = new Random();
        int amount = random.nextInt(10000);
        order.setAmount(amount);
        order.setOrderInfo("支付金额：" + ((double)amount / 100) + "元");
        order.setPayStatus(0);
        order.setCreateTime((int)(System.currentTimeMillis() / 1000));
        orderMapper.addOrder(order);

        return order;
    }

    public Order getOrder(int id) {
        if(id <= 0) {
            return null;
        }
        return orderMapper.getOrder(id);
    }

    public List<Order> getExpireOrders() {
        List<Order> orders = orderMapper.selectExpireOrders();
        for(Order order : orders) {
            //order.SS = "SSS";
        }

        return orders;
    }

    //设置订单为支付
    public boolean setOrderPayed(int id) {
        if(id <= 0) {
            return false;
        }
        return orderMapper.payOrder(id);
    }

    //设置订单为未支付
    public boolean setOrderUnPay(int id) {
        if(id <= 0) {
            return false;
        }
        return orderMapper.setUnpayOrder(id);
    }
}
